<template>
  <a-layout-header class="header">
    <div class="header-content">
      <!-- 左侧：Logo和标题 -->
      <div class="header-left">
        <RouterLink to="/" class="logo-link">
          <img class="logo" src="@/assets/logo.png" alt="Logo" />
          <h1 class="site-title">AI应用生成平台</h1>
        </RouterLink>
      </div>

      <!-- 中间：导航菜单 -->
      <div class="header-center">
        <a-menu
          :selected-keys="selectedKeys"
          mode="horizontal"
          @click="handleMenuClick"
          class="nav-menu"
        >
          <a-menu-item v-for="item in menuItems" :key="item.path" class="nav-item">
            <span style="font-size: 16px">{{ item.name }}</span>
          </a-menu-item>
        </a-menu>
      </div>

      <!-- 右侧：用户操作区域 -->
      <div class="header-right">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id" class="user-info">
            <a-dropdown placement="bottomRight">
              <div class="user-dropdown">
                <a-avatar
                  :src="loginUserStore.loginUser.userAvatar"
                  :size="40"
                  class="user-avatar"
                />
                <span class="username">{{ loginUserStore.loginUser.userName ?? '无名' }}</span>
              </div>
              <template #overlay>
                <a-menu class="user-dropdown-menu">
                  <!--                  <a-menu-item key="profile">-->
                  <!--                    <UserOutlined />-->
                  <!--                    个人中心-->
                  <!--                  </a-menu-item>-->
                  <!--                  <a-menu-item key="settings">-->
                  <!--                    <SettingOutlined />-->
                  <!--                    设置-->
                  <!--                  </a-menu-item>-->
                  <!--                  <a-menu-divider />-->
                  <a-menu-item key="logout" @click="handleLogout">
                    <LogoutOutlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" class="login-btn" href="/user/login">登录</a-button>
          </div>
        </div>
      </div>
    </div>
  </a-layout-header>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { useRouter } from 'vue-router'
import { routes } from '@/router/routes.ts'
import { useLoginUserStore } from '@/stores/loginUser.ts'
import { message } from 'ant-design-vue'
import { LogoutOutlined } from '@ant-design/icons-vue'
import { userLogout } from '@/api/userController.ts'
import { checkAccess } from '@/utils/access.ts'

const loginUserStore = useLoginUserStore()
const router = useRouter()

const menuItems = computed(() => {
  return routes.filter((item) => {
    if (item.meta.hideInMenu) {
      return false
    }

    // 根据权限过滤菜单，有权限则返回 true，保留该菜单
    return checkAccess(loginUserStore.loginUser, item.meta?.access as string)
  })
})

// 当前选中菜单
const selectedKeys = ref<string[]>(['/'])

// 监听路由变化，更新当前选中菜单
router.afterEach((to) => {
  selectedKeys.value = [to.path]
})

// 处理菜单点击
const handleMenuClick = (e: { key: string }) => {
  const key = e.key
  if (key.startsWith('/')) {
    router.push({ path: key })
  }
}

// 处理退出登录
const handleLogout = async () => {
  const res = await userLogout()
  if (res.data.code === 0) {
    await loginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成果')
    await router.push('/user/login')
  } else {
    message.error('退出登录失败，' + res.data.message)
  }
}
</script>

<style scoped>
.header {
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  height: 64px;
}

.header-content {
  max-width: 1400px;
  margin: 0 auto;
  height: 100%;
  display: flex;
  align-items: center;
  padding: 0 32px;
}

/* 左侧 Logo 区域 */
.header-left {
  flex-shrink: 0;
  margin-right: 48px; /* 与菜单保持适当距离 */
}

.logo-link {
  display: flex;
  align-items: center;
  text-decoration: none;
  transition: transform 0.3s ease;
}

.logo-link:hover {
  transform: scale(1.02);
}

.logo {
  height: 40px;
  width: 40px;
  border-radius: 8px;
  object-fit: cover;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  margin-right: 16px;
}

.site-title {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* 中间导航菜单 */
.header-center {
  flex: 1;
  min-width: 140px;
}

.nav-menu {
  border-bottom: none !important;
}

/* 右侧用户区域 */
.header-right {
  flex-shrink: 0;
}

.user-login-status {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 4px 8px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.user-dropdown:hover {
  background-color: rgba(0, 0, 0, 0.04);
}

.user-avatar {
  transition: all 0.3s ease;
  border: 2px solid #f0f0f0;
}

.user-avatar:hover {
  border-color: #1890ff;
  transform: scale(1.05);
}

.username {
  font-size: 14px;
  color: #333;
  font-weight: 500;
}

.dropdown-icon {
  color: #999;
  font-size: 12px;
  transition: transform 0.3s ease;
}

.user-dropdown:hover .dropdown-icon {
  color: #1890ff;
}

.user-dropdown-menu {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.login-btn {
  height: 36px;
  padding: 0 20px;
  font-size: 14px;
  font-weight: 500;
  border-radius: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.login-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(24, 144, 255, 0.3);
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .header-content {
    padding: 0 24px;
  }

  .header-left {
    margin-right: 32px;
  }

  .nav-item {
    margin: 0 16px;
  }
}

@media (max-width: 768px) {
  .header-content {
    padding: 0 16px;
  }

  .site-title {
    display: none;
  }

  .logo {
    height: 32px;
    width: 32px;
  }

  .header-left {
    margin-right: 24px;
  }

  .nav-item {
    margin: 0 12px;
    font-size: 14px;
  }

  .user-avatar {
    width: 32px;
    height: 32px;
  }

  .username {
    display: none;
  }

  .dropdown-icon {
    display: none;
  }

  .login-btn {
    height: 32px;
    padding: 0 16px;
    font-size: 13px;
  }
}
</style>
